/* eslint-disable no-unused-vars */
import {
  dclassMap,
  dclassCodeMap,
  resourceBaseUrl
} from './constant'
import {
  parseTime,
  getDateDiff,
  isNumber
} from '@/utils'
import { hexToSingle } from './blootooth'
const boolEnums = ['true', true, '"true"', 1, '1', 1.0, '1.0', 2, '2', 1.0, '1.0', 255, '255.0']

/**
 * 获取设备实时状态
 * @param {*} value
 * @param {*} rtuPortDisplay
 * @returns
 */
export function getDeviceState(value, rtuPortDisplay, states) {
  let result = rtuPortDisplay.default_val || '--'
  let { kunit, offset } = rtuPortDisplay
  if (rtuPortDisplay.datatype === 'enum') {
    if (states) {
      result = getDeviceEnumState(rtuPortDisplay, states)
    }
  } else if (rtuPortDisplay.datatype === 'mix') {
    const expression = JSON.parse(rtuPortDisplay.expression)
    result = ''
    expression.forEach((item) => {
      result += parseInt(states && states[item.namekey] || 0) + item.unit
    })
  } else if (rtuPortDisplay.datatype === 'pwmNew') {
    if (states) {
      const idx = rtuPortDisplay.namekey.split('_')[1]
      let p1 = states['p' + idx] || 0
      p1 = p1 + '%'
      let d1 = states['d' + idx] || 0
      d1 = d1 + 's'

      return p1 + ' | ' + d1
    }
    return ''
  } else {
    try {
      if (value !== undefined || rtuPortDisplay.expression) {
        switch (rtuPortDisplay.datatype) {
          case 'int':
          case 'float':
            if (rtuPortDisplay.expression) {
              if (['A1', 'A2'].includes(rtuPortDisplay.namekey)) {
                let A = states['A'] || 0
                A = Math.round(A * 0.01)
                result = 0
                console.log(A)
                if (rtuPortDisplay.namekey === 'A1') {
                  if (A >= 0 && A <= 90) {
                    result = Math.round(Math.abs(A - 90) / 90 * 100)
                  }
                  if (A >= 270 && A <= 360) {
                    result = Math.round(Math.abs(A - 270) / 90 * 100)
                  }
                }

                if (rtuPortDisplay.namekey === 'A2') {
                  if (A >= 90 && A <= 180) {
                    result = Math.round(Math.abs(A - 90) / 90 * 100)
                  }
                  if (A >= 180 && A <= 270) {
                    result = Math.round(Math.abs(A - 270) / 90 * 100)
                  }
                }
              } else {
                const format = JSON.parse(rtuPortDisplay.expression)
                const operator = format.operator
                const val = format.value
                const names = format.names
                result = value
                if (operator === '>') {
                  result = value > val ? names[1] : names[0]
                } else if (operator === '&') {
                  result = value & val ? names[1] : names[0]
                } else if (operator === '+') {
                  const { type, namekeys } = format
                  result = ''

                  if (type === 'float') {
                    namekeys.forEach((item) => {
                      if (states[item] > 0) {
                        result += (+states[item]).toString(16)
                      }
                    })
                    result = hexToSingle(result)
                  }
                  if (type === 'hex') {
                    namekeys.forEach((item) => {
                      if (states[item] >= 0) {
                        result += (+states[item]).toString(16).padStart(4, 0)
                      }
                    })
                    result = parseInt(result, 16)
                  }
                }

                result = result * kunit
              }
            } else {
              if (typeof (value) === 'string') {
                value = value.replace(/"/g, '')
              }
              kunit = kunit || 1
              offset = offset || 0
              result = value !== undefined ? (Number(value) * kunit - offset).toFixed(rtuPortDisplay.fmt) : '--'
            }
            break
          case 'bool': {
            const bool_format_arr = rtuPortDisplay.bool_format_arr.split(',')
            if (['打开', '启动'].includes(bool_format_arr[0])) {
              value = [0, '0'].includes(value) ? 0 : 1
            } else {
              value = boolEnums.includes(value) ? 1 : 0
            }

            result = bool_format_arr[value]
          }

            break
          case 'pwm':
            result = getPwmInfo(value)
            break
          default:
            result = value.replace(/"/g, '')
            break
        }
      }
      // boolEnums
      // eslint-disable-next-line no-empty
    } catch (err) {}
  }

  if (rtuPortDisplay.namekey === 'rssi' && rtuPortDisplay.name.indexOf('值') === -1) {
    if (isNumber(result) && result < 0) {
      result = getRssiStep(result)
    }
  }

  return result
}

export function getRssiStep(value) {
  if (value >= -90) {
    return '优'
  }
  if (value >= -110 && value < -90) {
    return '中'
  }
  return '差'
}

export function getDeviceEnumState(rtuPortDisplay, states) {
  const enums = JSON.parse(rtuPortDisplay.bool_format_arr)
  const namekeys = enums.namekeys
  const values = enums.values
  const length = namekeys.length

  if (values.length > namekeys.length) {
    return values[parseInt(states[namekeys[0]])]
  } else {
    for (let i = 0; i < length; i++) {
      if (i < length - 1) {
        if (Object.prototype.hasOwnProperty.call(states, namekeys[i]) && boolEnums.includes(states[namekeys[i]])) {
          return values[i]
        }
      } else {
        return values[i]
      }
    }
  }
}

/**
 * 计算脉冲值
 * @param {*} value
 * @returns
 */
export function getPwmInfo(value) {
  const hex = (+value).toString(16)
  let cycle = 60
  let pwm = 50
  if (hex.length === 1) {
    cycle = 0
    pwm = parseInt(hex[0], 16)
  }
  if (hex.length === 2) {
    cycle = parseInt(hex[0], 16)
    pwm = parseInt(hex[1], 16)
  }
  if (hex.length === 3) {
    cycle = parseInt(hex[0], 16)
    pwm = parseInt(hex.substr(1, 2), 16)
  }
  if (hex.length === 4) {
    cycle = parseInt(hex.substr(0, 2), 16)
    pwm = parseInt(hex.substr(2, 2), 16)
  }

  return cycle + 's | ' + pwm + '%'
}

/**
 * 将设备以dclass分组
 * @param {*} devices
 * @returns
 */
export function getDclasses(devices) {
  const map = {}
  const result = []
  devices = devices.filter(val => {
    return +val.status === 1
  })
  for (const item of devices) {
    if (!map[item.product_type]) {
      result.push({
        dclass: item.product_type,
        dclassName: dclassMap[item.product_type].name || '',
        data: [item]
      })
      map[item.product_type] = item
    } else {
      for (var j = 0; j < result.length; j++) {
        var dj = result[j]
        if (dj.dclass === item.product_type) {
          dj.data.push(item)
          break
        }
      }
    }
  }
  return result
}

/**
 * 格式化设备最后采集时间
 * @param {*} value
 * @returns
 */
export function getDatatime(value, states) {
  if (value) {
    const currentYear = +value.substr(0, 4)
    if (currentYear !== new Date().getFullYear()) {
      return value.substr(0, 10)
    }
    const currentDate = value.substr(5, 5)
    const currentTime = value.substr(11)

    var date = new Date()
    let month = date.getMonth() + 1
    let day = date.getDate()

    month = month < 10 ? '0' + month : month
    day = day < 10 ? '0' + day : day

    if (currentDate === month + '-' + day) {
      return currentTime
    }

    return currentDate
  }

  return '01-01'
}

/**
 * 显示弹出视频框
 * @param {*} serialno
 * @param {*} validCode
 * @param {*} accessToken
 */
export function showVideo(serialno, validCode, accessToken) {
  const url = `https://open.ys7.com/ezopen/h5/live?autoplay=1&audio=1&validCode=${validCode}&accessToken=${accessToken}&hd=1&deviceSerial=${serialno}&channelNo=1`
  window.open(url, '_blank', 'width=600,height=400')
}

/**
 * 显示设备的实时状态
 * @param {*} device
 * @returns
 */
export function getDeviceStatus(device, statusMapProps = null) {
  let status = 'd_break'
  const states = device.states || {}
  let isOffline = true
  const now = parseTime(new Date().getTime())
  if (states.datatime) {
    isOffline = getDateDiff(states.datatime, now, 'hour') > 4
  }

  const version = device.product.model.replace('.0', '')
  if (!statusMapProps && !device.status_map) {
    switch (+device.product_type) {
      case dclassCodeMap.qxz:
      case dclassCodeMap.sqz:
      case dclassCodeMap.gcwd:
      case dclassCodeMap.gd:
      case dclassCodeMap.ndvi:
      case dclassCodeMap.gps:
      case dclassCodeMap.cqz:
      case dclassCodeMap.bzy:
      case dclassCodeMap.scd:
      case dclassCodeMap.zyp:
      case dclassCodeMap.gdgcwd:
      case dclassCodeMap.wg:
      case dclassCodeMap.llj:
      case dclassCodeMap.ylj:
      case dclassCodeMap.pressure:
      case dclassCodeMap.spj:
        if (isOffline) {
          status = 'd_break'
        } else {
          if ((states.status && states.status === 'online') || !isOffline) {
            status = 'd_run'
          }
        }
        break
      case dclassCodeMap.zk:
        if ((states.status && states.status === 'online') || !isOffline) {
          if (boolEnums.includes(states.Fault)) {
            status = 'd_trouble'
          } else {
            status = 'd_close'
            if (boolEnums.includes(states.Lifting) || boolEnums.includes(states.Falling)) {
              status = 'd_run'
            }
          }
        }
        break
      case dclassCodeMap.spjk:
        status = 'd_run'
        break
      case dclassCodeMap.pg:
        if (version === 'V1') {
          if (states.status === 'online') {
            if (boolEnums.includes(states.SafeCircuit_State)) {
              status = 'd_trouble'
            } else if (boolEnums.includes(states.Forward_HMI) || boolEnums.includes(states.Backward_HMI)) {
              status = boolEnums.includes(states.Forward_HMI) ? 'd_forward' : 'd_reverse'
            } else {
              status = 'd_close'
            }
          }
        }
        if (version === 'V2' || version === 'V4') {
          if (states.status === 'online' && !isOffline) {
            const forward_sta = states.Fwd || states.FWD_HMI
            const reverse_sta = states.Rev || states.REV_HMI
            const systemRun = (forward_sta === true || +forward_sta === 255) || (reverse_sta === true || +reverse_sta === 255) || forward_sta === undefined || reverse_sta === undefined
            if (systemRun && boolEnums.includes(states.TROUBLE_HMI)) {
              status = 'd_trouble'
            } else if (boolEnums.includes(forward_sta) || boolEnums.includes(reverse_sta)) {
              status = boolEnums.includes(forward_sta) ? 'd_forward' : 'd_reverse'
            } else {
              status = boolEnums.includes(states.System_Run) ? 'd_run' : 'd_close'
            }
          }
        }
        if (version === 'V3') {
          if (states.status === 'online') {
            if (boolEnums.includes(states.SafeCircuit_State)) {
              status = 'd_trouble'
            } else if (boolEnums.includes(states.Fwd_Sta) || boolEnums.includes(states.Bwd_Sta)) {
              status = boolEnums.includes(states.Fwd_Sta) ? 'd_forward' : 'd_reverse'
            } else {
              status = 'd_close'
            }
          }
        }
        break
      case dclassCodeMap.sb:
        if (version === 'V1') {
          if (states.status === 'online') {
            if (boolEnums.includes(states.fault1) || boolEnums.includes(states.fault2)) {
              status = 'd_trouble'
            } else {
              status = (boolEnums.includes(states.running1)) || (boolEnums.includes(states.running2) || boolEnums.includes(states.FC1)) || (boolEnums.includes(states.FC2)) || (boolEnums.includes(states.FC3) || boolEnums.includes(states.PF1)) || (boolEnums.includes(states.PF2)) || (boolEnums.includes(states.PF3))
                ? 'd_run' : 'd_close'
            }
          }
        }
        if (version === 'V2') {
          if (states.status === 'online') {
            if (boolEnums.includes(states.TROUBLE_HMI || states.a4 > 3000)) {
              status = 'd_trouble'
            } else {
              status = boolEnums.includes(states.RUN_HMI || Boolean((states.DO || 0) & 4)) ? 'd_run' : 'd_close'
            }
          }
        }
        if (version === 'V4') {
          if (states.status === 'online') {
            if (boolEnums.includes(states.StatErr)) {
              status = 'd_trouble'
            } else {
              status = boolEnums.includes(states.StatRun) ? 'd_run' : 'd_close'
            }
          }
        }
        if (version === 'V5') {
          if (!isOffline) {
            if (boolEnums.includes(states.水泵故障)) {
              status = 'd_trouble'
            } else {
              status = boolEnums.includes(states.水泵启停) ? 'd_run' : 'd_close'
            }
          }
        }
        break
      case dclassCodeMap.qf:
        if (version === 'V1') {
          if (states.datatime && getDateDiff(states.datatime, now, 'hour') <
							24) {
            status = 'd_run'

            if (states.VPercent < 75) {
              status = 'd_v_2'
            }
            if (states.VPercent < 50) {
              status = 'd_v_3'
            }
            if (states.VPercent < 25) {
              status = 'd_v_4'
            }
          } else {
            status = 'd_break'
          }
        }
        if (version === 'V2') {
          if (states.datatime && getDateDiff(states.datatime, now, 'hour') <
							24) {
            status = 'd_run'

            if (states.VPercent < 75) {
              status = 'd_v_2'
            }
            if (states.VPercent < 50) {
              status = 'd_v_3'
            }
            if (states.VPercent < 25) {
              status = 'd_v_4'
            }
          } else {
            status = 'd_break'
          }
        }
        break
      case dclassCodeMap.sf:
        if (version === 'V1') {
          if (states.status === 'online') {
            status = (boolEnums.includes(states.fertiStart)) ? 'd_run' : 'd_close'
          }
        }
        if (version === 'V2') {
          if (states.status === 'online') {
            status = (boolEnums.includes(states.FertPump_State)) ? 'd_run' : 'd_close'
          }
        }
        if (version === 'V3') {
          if (!isOffline) {
            status = (boolEnums.includes(states.Stat)) ? 'd_run' : 'd_close'
          }
        }
        if (version === 'V4') {
          if (states.datatime && getDateDiff(states.datatime, now, 'hour') <
							6) {
            status = (boolEnums.includes(states.注肥启停)) ? 'd_run' : 'd_close'
          }
        }
        break
      case dclassCodeMap.fm:
        status = (boolEnums.includes(states.DO)) ? 'd_run' : 'd_close'
        break
      case dclassCodeMap.df:
        if (version === 'V1') {
          status = 'd_break'
          if (states.status === 'online') {
            let OpenStat = states.OpenStat
            let CloseStat = states.CloseStat
            if (states.DI) {
              OpenStat = !(states.DI & 1)
              CloseStat = !(states.DI & 2)
            }
            if (boolEnums.includes(OpenStat) || boolEnums.includes(CloseStat)) {
              if (boolEnums.includes(OpenStat)) {
                status = 'd_run'
              }
              if (boolEnums.includes(CloseStat)) {
                status = 'd_close'
              }
            } else {
              status = 'd_pause'
            }
          }
        }
        if (version === 'V2') {
          if (isOffline) {
            status = 'd_break'
          } else {
            const a1 = +states.a1
            const a2 = +states.a2
            if ([1, 2].includes(a1) || [1, 2].includes(a2)) {
              status = 'd_run'
            } else if ([0, 3].includes(a1) || [0, 3].includes(a2)) {
              status = 'd_pause'
            } else {
              status = 'd_trouble'
            }
          }
        }
        break
      case dclassCodeMap.fkq:
        if (version === 'V1') {
          status = states.DO && Number(states.DO) > 0 ? 'd_run' : 'd_close'
          const DO1 = boolEnums.includes(states['DO.0']) ? 1 : 0
          const DO2 = boolEnums.includes(states['DO.1']) ? 1 : 0
          const DO3 = boolEnums.includes(states['DO.2']) ? 1 : 0
          const DO4 = boolEnums.includes(states['DO.3']) ? 1 : 0
          status = `${DO1}${DO2}${DO3}${DO4}`
        }
        if (version === 'V2') {
          status = 'd_close'
          if (states && (states.DO & 8 || states.DO & 12)) {
            status = 'd_run'
          }
          const DO3 = (states.DO & 4) > 0 ? 1 : 0
          const DO4 = (states.DO & 8) > 0 ? 1 : 0

          let a3 = 0
          if (states && states.a3) {
            const a3Number = Number(states.a3)
            if (a3Number >= 100 && a3Number <= 3000) {
              a3 = 2
            }
            if (a3Number > 3000) {
              a3 = 1
            }
            if (a3Number < 100) {
              a3 = 0
            }
          }
          let a4 = 0
          if (states && states.a4) {
            const a4Number = Number(states.a4)
            if (a4Number >= 100 && a4Number <= 3000) {
              a4 = 2
            }
            if (a4Number > 3000) {
              a4 = 1
            }
            if (a4Number < 100) {
              a4 = 0
            }
          }
          return `${DO3}${DO4}${a3}${a4}`
        }
        if (version === 'V3') {
          const GateCtrlStat = states.GateCtrlStat || 0
          const GateStat = states.GateStat || 0

          return `${GateCtrlStat}${GateStat}`
        }
        if (version === 'V4') {
          const DO1 = boolEnums.includes(states['电磁阀1']) ? 1 : 0
          const DO2 = boolEnums.includes(states['电磁阀2']) ? 1 : 0
          const DO3 = boolEnums.includes(states['电磁阀3']) ? 1 : 0
          const DO4 = boolEnums.includes(states['电磁阀4']) ? 1 : 0
          status = `${DO1}${DO2}${DO3}${DO4}`
        }
        if (version === 'V5') {
          status = states.DO && Number(states.DO) > 0 ? 'd_run' : 'd_close'
          const DO1 = states['DO'] & 1 ? 1 : 0
          const DO2 = states['DO'] & 2 ? 1 : 0
          const DO3 = states['DO'] & 4 ? 1 : 0
          const DO4 = states['DO'] & 8 ? 1 : 0
          status = `${DO1}${DO2}${DO3}${DO4}`
        }
        if (version === 'V6') {
          let states_a1 = states['a1']
          let states_a2 = states['a2']
          // console.log(states_a1,states_a2)
          if (states_a1 !== undefined) {
            states_a1 = +states_a1
          }
          if (states_a2 !== undefined) {
            states_a2 = +states_a2
          }
          status = states.DO && Number(states.DO) > 0 ? 'd_run' : 'd_close'
          const DO1 = boolEnums.includes(states['CH1_SW']) ? 1 : 0
          const DO2 = boolEnums.includes(states['CH2_SW']) ? 1 : 0
          if (states_a1 === 0 || states_a2 === 0) {
            status = 'd_run'
          }
        }
        break
      case dclassCodeMap.dg:
        status = 'd_close'
        break
    }
  } else {
    const status_map = statusMapProps || device.status_map
    if (status_map.d_run) {
      status = states[status_map.d_run] && boolEnums.includes(states[status_map.d_run]) ? 'd_run' : 'd_close'
    }
    if (status_map.d_trouble) {
      const status_map_trouble_arr = status_map.d_trouble.split(',')
      status_map_trouble_arr.forEach((item) => {
        if (item.indexOf('Alarm') > -1) {
          const value = (+states[item]).toString(2).padStart(16, 0)
          const res = createArr(value, 0, 4, 4)
          const alarms = ['0010', '0011', '0100']
          const alarm = alarms.includes(res[0]) || alarms.includes(res[1]) || alarms.includes(res[
            2]) || alarms.includes(res[3])
          if (alarm) {
            status = 'd_trouble'
          }
        } else {
          if (boolEnums.includes(states[item])) {
            status = 'd_trouble'
          }
        }
      })
    }
  }

  return status
}

export function createArr(str, start, end, unit) {
  const arr = [] // 保存每次截取后的字符串
  const counts = Math.ceil(str.length / unit) // 循环截取的次数,向上取整
  for (let i = 0; i < counts; i++) {
    const newStr = str.substring(start, end)
    arr.push(newStr)
    start = end
    end += unit
  }
  return arr
}

export function getDeviceEnumValues(device) {
  const rtuPortDisplays = device.rtuPortDisplays.filter(val => {
    return val.datatype === 'enum'
  })
  // console.log(rtuPortDisplays)
}

/**
 * 给设备加上最新状态
 * @param {*} devices
 * @param {*} states
 * @returns
 */
export function mergeDevicesStates(devices, states) {
  devices.map(item => {
    if (states[item.serialno]) {
      item.states = Object.assign(item.states, states[item.serialno])
    }
  })

  return devices
}

/**
 * 获取设备对应状态图片
 * @param {*} currentDevice
 */
export function getDeviceImage(currentDevice) {
  if (+currentDevice.product_type === 14 && +currentDevice.product_id === 113) {
    return `https://api.zesi.com.cn/storage/fkq/${getDeviceStatus(currentDevice)}.png`
  }
  return `${resourceBaseUrl}${dclassMap[currentDevice.product_type].code}/${getDeviceStatus(currentDevice)}.png`
}

export function getValveImage(device, port) {
  let status
  if (!port) {
    status = getValveStatus(device, 3) > 0 || getValveStatus(device, 4) > 0 ? 'd_run' : 'd_close'
  } else {
    status = getValveStatus(device, port) > 0 ? 'd_run' : 'd_close'
  }
  return `${resourceBaseUrl}${dclassMap[13].code}/${status}.png`
}

export function getValveStatus(device, port) {
  if (+port > 0) {
    const DO = (device && device.states && device.states.DO) || 0
    const val = Math.pow(2, port - 1)
    return DO & val
  }
  return (device.states && device.states.GateCtrlStat && +device.states.GateCtrlStat) ? 1 : 0
}

export function getFmImage(device, port) {
  const status = getFmStatus(device, port)
  return `${resourceBaseUrl}${dclassMap[13].code}/${status}.png`
}

export function getQfImage(device, port) {
  const status = getDeviceStatus(device) || 'd_close'
  return `${resourceBaseUrl}${dclassMap[101].code}/${status}.png`
}

export function getFmStatus(device, port) {
  const states = device.states
  let status = 'd_close'
  if (port > 0) {
    const namekey = 'a' + port
    if (states && states[namekey]) {
      status = 'd_trouble'
      if (Number(states[namekey]) < 100) {
        status = 'd_close'
      }
      if (Number(states[namekey]) > 3000) {
        status = 'd_run'
      }
    }
  } else {
    status = states['GateStat'] && +states['GateStat'] ? 'd_run' : 'd_close'
  }

  return status
}

/**
 * 获取所有要订阅的设备序列号
 * @param {*} devices
 * @returns
 */
export function getMqttSerialnos(devices) {
  const serialnos = devices.map(item => item.serialno)
  const devices_has_child = devices.filter(val => {
    return val.devices && val.devices.length > 0
  })
  let result = []
  devices_has_child.forEach(item => {
    result = serialnos.concat(item.devices.map(val => val.serialno))
  })

  return result
}

/**
 * 生成计划指令
 * @param {*} formData
 * @returns
 */
export function formatPlanData(formData, userId) {
  const result = {
    name: formData.name,
    serialno: formData.serialno,
    namekey: 'sys_cron',
    expression: '>=',
    val: formData.starttime
  }
  const devices = []
  let ctlGroup = 0
  if (formData.pump_serialno) {
    devices.push({
      actionStart: `[{"namekey":"PumpStart","params":1,"traceId":"${userId}"}]`,
      actionStop: `[{"namekey":"PumpStop","params":1,"traceId":"${userId}}"]`,
      ctlGroup: ++ctlGroup,
      dclass: +dclassCodeMap.sb,
      delaySec: 0,
      idx: 0,
      serialno: formData.pump_serialno,
      val: 0,
      expression: '=',
      namekey: 'sys_runtime'
    })
  }
  if (formData.cells.length > 0) {
    formData.cells.forEach(val => {
      const valves = val.devices
      ++ctlGroup
      valves.forEach((cvalue, cindex) => {
        const traceId = userId + '-' + Math.random().toString().substring(2, 8)
        let actionStart = `[{"namekey":"DO${cvalue.port * 2 + 5}","params":true,"traceId":"${traceId}"}]`
        let actionStop = `[{"namekey":"DO${cvalue.port * 2 + 6}","params":true,"traceId":"${traceId}"}]`
        if (+cvalue.port === 0) {
          actionStart = `[{"namekey":"GateCtrl","params":1,"traceId":"${traceId}"}]`
          actionStop = `[{"namekey":"GateCtrl","params":0,"traceId":"${traceId}"}]`
        }

        devices.push({
          actionStart,
          actionStop,
          ctlGroup: +ctlGroup,
          dclass: +dclassCodeMap.fm,
          delaySec: val.delaySec,
          idx: +cindex,
          serialno: cvalue.rtu_serialno,
          val: +val.val,
          expression: '=',
          namekey: 'sys_runtime'
        })
      })
    })
    result.devices = devices
    return result
  }
}

/**
 * 生成计划球阀计划指令
 * @param {*} formData
 * @returns
 */
export function formatPlanDataForQf(formData, userId) {
  const result = {
    name: formData.name,
    serialno: formData.serialno,
    namekey: 'sys_cron',
    expression: '>=',
    val: formData.starttime
  }
  const devices = []
  let ctlGroup = 0
  if (formData.pump_serialno) {
    devices.push({
      actionStart: `[{"namekey":"PumpStart","params":1,"traceId":"${userId}"}]`,
      actionStop: `[{"namekey":"PumpStop","params":1,"traceId":"${userId}}"]`,
      ctlGroup: ++ctlGroup,
      dclass: +dclassCodeMap.sb,
      delaySec: 0,
      idx: 0,
      serialno: formData.pump_serialno,
      val: 0,
      expression: '=',
      namekey: 'sys_runtime'
    })
  }
  if (formData.cells.length > 0) {
    formData.cells.forEach(val => {
      const valves = val.devices
      const openNamekey = 'Open' + val.port
      ++ctlGroup
      valves.forEach((cvalue, cindex) => {
        const traceId = userId + '-' + Math.random().toString().substring(2, 8)
        const actionStart = `[{"namekey":"${openNamekey}","params":100,"traceId":"${traceId}"}]`
        const actionStop = `[{"namekey":"Close","params":1,"traceId":"${traceId}"}]`

        devices.push({
          actionStart,
          actionStop,
          ctlGroup: +ctlGroup,
          dclass: +dclassCodeMap.qf,
          delaySec: val.delaySec,
          idx: +cindex,
          serialno: cvalue.rtu_serialno,
          val: +val.val,
          expression: '=',
          namekey: 'sys_runtime'
        })
      })
    })
    result.devices = devices
    return result
  }
}

export function getPlanStatus(item) {
  let res = {
    name: '待执行',
    class: 'color-info'
  }
  if (item.status === 2) {
    res = {
      name: '执行中',
      class: 'color-primary'
    }
  } else if (item.status === 3) {
    res = {
      name: '已完成',
      class: 'color-success'
    }
  } else if (item.status === 4) {
    res = {
      name: '已取消',
      class: 'color-warning'
    }
  }

  return res
}

/**
 * 生成计划指令
 * @param {*} formData
 * @returns
 */
export function formatPlanDataForValves(formData, userId) {
  const result = {
    serialno: formData.serialno,
    namekey: 'sys_cron',
    expression: '>='
  }
  const devices = []
  const valves = formData.valves || []
  const ctlGroup = 0
  valves.forEach((cvalue, cindex) => {
    const traceId = userId + '-' + Math.random().toString().substring(2, 8)

    let actionStart = `[{"namekey":"DO${cvalue.port * 2 + 5}","params":true,"traceId":"${traceId}"}]`
    let actionStop = `[{"namekey":"DO${cvalue.port * 2 + 6}","params":true,"traceId":"${traceId}"}]`
    if (+cvalue.port === 0) {
      actionStart = `[{"namekey":"GateCtrl","params":1,"traceId":"${traceId}"}]`
      actionStop = `[{"namekey":"GateCtrl","params":0,"traceId":"${traceId}"}]`
    }

    devices.push({
      actionStart,
      actionStop,
      ctlGroup: +ctlGroup,
      dclass: +dclassCodeMap.fm,
      delaySec: formData.delaySec,
      idx: +cindex,
      serialno: cvalue.rtu_serialno,
      val: +formData.val,
      expression: '=',
      namekey: 'sys_runtime'
    })
  })

  result.devices = devices
  return result
}

/**
 * 生成计划指令
 * @param {*} formData
 * @returns
 */
export function formatPlanDataForValvesNew(formData, userId) {
  const result = {
    serialno: formData.serialno,
    namekey: 'sys_cron',
    expression: '>='
  }
  const devices = []
  const valves = formData.valves || []
  const ctlGroup = 0
  valves.forEach((cvalue, cindex) => {
    const traceId = userId + '-' + Math.random().toString().substring(2, 8)

    const actionStart = `[{"namekey":"CH${cvalue.port}_OPEN","params":1,"traceId":"${traceId}"}]`
    const actionStop = `[{"namekey":"CH${cvalue.port}_CLOSE","params":0,"traceId":"${traceId}"}]`

    devices.push({
      actionStart,
      actionStop,
      ctlGroup: +ctlGroup,
      dclass: +dclassCodeMap.fm,
      delaySec: formData.delaySec,
      idx: +cindex,
      serialno: cvalue.rtu_serialno,
      val: +formData.val,
      expression: '=',
      namekey: 'sys_runtime'
    })
  })

  result.devices = devices
  return result
}

// 格式化喷灌机速率
export function formatSprinklerRate(value, prefix = '00') {
  const rateHex = value.toString(16)
  let result = value > 15 ? rateHex : `0${rateHex}`
  result = parseInt(prefix + result, 16)

  return result
}
